﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Linq;

namespace Falcon.Core.Domain.Extensions
{
	using System.Collections.Generic;
	using Falcon.Core.Domain.Enums;

	public static class UserPermissionExtension
	{
		/// <summary>
		/// Determines whether the permissions has the role
		/// </summary>
		/// <param name="permissions">The permissions.</param>
		/// <param name="role">The role.</param>
		/// <returns>
		/// 	<c>true</c> if permissions has the role; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(this IList<UserPermission> permissions, string role)
		{
			return permissions.Count(x => x.Role.Title == role) > 0;
		}

		/// <summary>
		/// Determines whether the permissions has the role
		/// </summary>
		/// <param name="permissions">The permissions.</param>
		/// <param name="role">The role.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if permissions has the role; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(this IList<UserPermission> permissions, string role, UserPermissionActions action)
		{
			return permissions.Count(x => x.Role.Title == role && (x.Action & action) == action) > 0;
		}

		/// <summary>
		/// Determines whether the permissions has the role and action.
		/// </summary>
		/// <param name="permissions">The permissions.</param>
		/// <param name="role">The role.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if the permissions has the role and action; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(this Dictionary<string, UserPermission> permissions, string role, int action)
		{
			return (permissions.ContainsKey(role) && ((int)permissions[role].Action & action) == action);
		}

		/// <summary>
		/// Determines whether the permissions has the role and action.
		/// </summary>
		/// <param name="permissions">The permissions.</param>
		/// <param name="role">The role.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if the permissions has the role and action; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(this Dictionary<string, UserPermission> permissions, string role, UserPermissionActions action)
		{
			return (permissions.ContainsKey(role) && (permissions[role].Action & action) == action);
		}
	}
}